<?php

	class Thumbnail{
	
		private $image;
		private $mimetype;
		private $imageproperties;
		private $path = "../img/";
	
	//	public function __construct (/*$file, $thumbNailSize = 100*/){
	/*		is_file($file) or die ("File doesn't exists");
			$this->imageproperties = getimagesize($file) or die ("Invalid file type");
			$this->mimetype = $this->imageproperties['mime'];
			
			switch ($this->imageproperties['mime']){
				case "image/jpeg":
				$this->image = imagecreatefromjpeg($file);
				break;
				
				case IMAGETYPE_GIF:
				$this->image = imagecreatefromgif($file);
				break;
				
				case IMAGETYPE_PNG:
				$this->image = imagecreatefrompng($file);
				break;
				
				default:
				die ("Can't create an image");
			}
			$this->createThumb($thumbNailSize);
		}
	*/
		private function createThumb($thumbNailSize){
			$srcW = $this->imageproperties[0];
			$srcH = $this->imageproperties[1];
			if($srcW > $thumbNailSize || $srcH > $thumbNailSize){
				$reduct = $this->calculateReduction($thumbNailSize);
				$desW = $srcW/$reduct;											
				$desH = $srcH/$reduct;
				$desW = round($desW);
				$desH = round($desH);
				$copy = imagecreatetruecolor($desW, $desH);
				imagecopyresampled($copy, $this->image, 0, 0, 0, 0, $desW, $desH, $srcW, $srcH) or die ("Image copy error");
				imagedestroy($this->image);
				$this->image = $copy;
			}
		}
		
		private function calculateReduction($thumbNailSize){
			$srcW = $this->imageproperties[0];
			$srcH = $this->imageproperties[1];
			if ($srcH > $srcW){
				$reduct = round($srcH/$thumbNailSize);  
			}
			else{
				$reduct = round($srcW/$thumbNailSize);
			}
			return $reduct;
		}
		
		public function getthumb(){	
		
			$mime = getimagesize($this->path.'5ZZ.jpg');
			$m = $mime['mime'];
		
			header ("Content-type: $m");
			
			switch ($mime['mime']){
			case IMAGETYPE_JPEG:
			imagejpeg($this->image);     				
			break;
			
			case IMAGETYPE_PNG:
			imagepng($this->image);
			break;
			
			case IMAGETYPE_GIF:
			imagegif($this->image);
			break;
			
			default:
			die ("Can't create an image");
			}
		}
		
		public function getmimetype(){
			return $this->mimetype;
		}
			
		public function __destruct(){
			if (isset($this->image)){
				imagedestroy($this->image);
			}
		}
		
	}

?>